package messages;

public class MessageUtils {
    
    private MessageUtils() {}
    
    public static int parseInt(byte[] data, int startIndex)
    {
        int retVal = 0;
        for (int i = 0;i < 4;i++)
        {
            int value = data[startIndex + i] << 8 * (i);
            value &= -1 & (255 << (i) * 8);
            retVal |= value;
        }
        return retVal;
    }
    
    public static void binaryPrint(int value)
    {
        for (int i = 0;i < 32;i++)
        {
            System.out.print( Math.abs((value >> (31 - i)) % 2 ));
        }
        System.out.println();
    }
    
    public static void insertInt(byte[] dataToModify, int index, int valueToInsert)
    {
        for (int i = 0;i < 4;i++)
        {
            dataToModify[index + i] = (byte) ( valueToInsert >> 8 * (i) );
        }
    }
    
    public static String byteArrayToString(byte[] data)
    {
    	StringBuffer dataStr = new StringBuffer();
    	
    	boolean isStart = true;
    	
    	for (byte b : data)
    	{
    		if (!isStart)
    		{
    			dataStr.append(", ");
    		}
    		dataStr.append(b);
    		
    		isStart = false;
    	}
    	
    	return dataStr.toString();
    }
    
}
